package com.snowy.hidden.user.mapper;

import com.snowy.framework.common.base.mapper.BaseMapper;
import com.snowy.hidden.user.domain.model.SysRole;
import com.snowy.hidden.user.domain.model.SysUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 角色(SysRole)表数据库访问层
 *
 * @author makejava
 * @since 2023-11-30 00:27:43
 */
@Mapper
public interface SysRoleMapper extends BaseMapper<SysRole> {
    /**
     * 查询用户角色列表
     * @param userId
     * @return
     */
    @Select("SELECT * FROM SYS_ROLE WHERE ID IN(\n" +
            "SELECT TARGET_ID FROM SYS_RELATION \n" +
            "WHERE CATEGORY='SYS_USER_HAS_ROLE' \n" +
            "AND OBJECT_ID=#{userId})")
    List<SysRole> listByUserId(@Param("userId")String userId);
    /**
     * 查询组织机构指定角色人员列表
     * @param orgId
     * @param roleCode
     * @return
     */
    @Select("select * from SYS_USER su where USER_STATUS='ENABLE' AND ORG_ID=#{orgId}\n" +
            "and exists(\n" +
            "select 1 from SYS_RELATION t1 inner join SYS_ROLE t2 on t1.TARGET_ID=t2.ID\n" +
            "where t1.CATEGORY='SYS_USER_HAS_ROLE' and t2.CODE=#{roleCode}\n" +
            "and su.ID=t1.OBJECT_ID)")
    List<SysUser> listOrgRoleUser(@Param("orgId")String orgId, @Param("roleCode")String roleCode);
}
